package com.example.simalaya.presenters;

import com.example.simalaya.interfaces.IRecommendViewCallback;
import com.example.simalaya.interfaces.IRecommendPresenter;
import com.example.simalaya.utils.Constants;
import com.example.simalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";

    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();

    private static volatile RecommendPresenter sInstance = null;
    private List<Album> albumList = new ArrayList<>();
    int times = 2; //api改变，一次只能加载10条推荐，故增加加载次数

    private RecommendPresenter(){
        times = 2;
    }

    /**
     * 获取单例对象
     * @return
     */
    public static RecommendPresenter getInstance(){
        if (sInstance==null){
            synchronized (RecommendPresenter.class){
                if (sInstance==null){
                    sInstance = new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }

    /**
     * 获取推荐内容,猜你喜欢
     * 接口：3.10.6 获取猜你喜欢专辑
     */
    @Override
    public void getRecommendList() {
        updateLoading();
        //获取推荐内容
        //封装参数
        Map<String, String> map = new HashMap<String, String>();
        //这个参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constants.RECOMMEND_COUNT + ""); //喜马拉雅api现在一次最多只能获取到10推荐
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                //获取成功
                if (gussLikeAlbumList != null) {
                    albumList.addAll(gussLikeAlbumList.getAlbumList()) ;
                    LogUtil.d(TAG,"size --> " + albumList.size());
//                    for (Album album : albumList) {
//                        System.out.println(album.getAlbumTitle());
//                    }
                    if (times >= 0){
                        getRecommendList();
                        times--;
                    }
                    //更新ui
                    //upRecommendData(albumList);
                    handlerRecommendResult(albumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                //获取失败
                LogUtil.d(TAG,"error --> " + i);
                LogUtil.d(TAG,"error Msg --> " + s);
                handlerError();
            }
        });

    }

    private void handlerError() {
        if (mCallbacks != null) {
            for (IRecommendViewCallback mCallback : mCallbacks) {
                mCallback.onNetworkError();
            }
        }
    }

    /**
     * 通知注册RecommendPresenterUI回调（IRecommendCallback）的所有组件（fragment/activity）
     * @param albumList
     */
    private void handlerRecommendResult(List<Album> albumList) {
//        //清除数据，测试空页面
//        albumList.clear();
        //通知UI更新
        if (albumList != null) {
            if (albumList.size() == 0) {
                for (IRecommendViewCallback callback : mCallbacks) {
                    callback.onEmpty();
                }
            }else {
                for (IRecommendViewCallback mCallback : mCallbacks) {
                    mCallback.onRecommendListLoaded(albumList);
                }
            }
        }

    }

    private void updateLoading() {
        for (IRecommendViewCallback mCallback : mCallbacks) {
            mCallback.onLoading();
        }
    }



    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null && !mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unRegisterViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }
}
